#include "SSTF.h"
#include <list>
#include "Position.h"
#include <iostream>

using namespace std;

SSTF::SSTF()
{
}

SSTF::~SSTF()
{
}

void SSTF::enqueue(Requisition pos)
{
	diskList.push_back(pos);
}

Requisition SSTF::next(Position at)
{
//	if (diskList.size() % 500 == 0)
//		cout << ": " << diskList.size() << endl;
	int curTrack = at.track;
	int minDist = 700000;
	Requisition ret;
	list<Requisition>::iterator it;
	list<Requisition>::iterator it2;
	for(it=diskList.begin();it!=diskList.end();it++)
	{
		int dist = it->track - curTrack;
		if(dist == 0) {
			ret = *it;
			it2 = it;
			break;
		}
		if(dist < 0 ) dist = -dist;
		if(dist < minDist)
		{
			minDist = dist;
//			cout << "minDist: " << minDist << endl;
			it2 = it;
			ret = *it;
		}
	}

//	cout << ": " << diskList.size() << endl;
	diskList.erase(it2);
//	cout << ": " << diskList.size() << endl;
	return ret;
	
}
